using System.Security.Claims;
using Timer = System.Windows.Forms.Timer;

namespace 第三次作业
{
    public partial class Form1 : Form
    {
        private Timer _timer;
        private int count = 0;
        private EventHandler<ClockEvent> _tickHandler;
        private EventHandler<ClockEvent> _alarmHandler;

        public Form1()
        {
            InitializeComponent();

            _timer = new Timer();
            _timer.Interval = 1000;
            _timer.Tick += Timer_Tick;

            _tickHandler = (sender, e) =>
            {
                tickMessage.Text = e.message;
            };

            _alarmHandler = async (sender, e) =>
            {
                alarmMessage.Text = e.message;
                btnStart.Enabled = true;
                btnStop.Enabled = false;

                await Task.Delay(1000);
                alarmMessage.Text = "";
            };
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            count ++;
            OnTick(new ClockEvent { message = "Tick" + count });
        }

        private void OnTick(ClockEvent e)
        {
            _tickHandler?.Invoke(this, e);
        }

        private void OnAlarm(ClockEvent e)
        {
            _alarmHandler?.Invoke(this, e);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;

            Alarm alarm = new Alarm();
            alarm.AlarmTime = DateTime.Now.AddSeconds(10);

            alarm.alarm += (s, ev) =>
            {
                OnAlarm(new ClockEvent { message = "Alarm" });
            };

            _timer.Start();
            alarm.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            _timer.Stop();
            tickMessage.Text = "stop";
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void tickMessage_Click(object sender, EventArgs e)
        {

        }

        private void alarmMessage_Click_1(object sender, EventArgs e)
        {

        }
    }
}